"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，查看基础信息(帮助信息/版本信息/所有帮助信息)
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，查看版本信息
    2、进入交互模式，查看帮助信息
    3、进入交互模式，查看详细帮助信息
Expect      :
    1、查看成功
    2、查看成功
    3、查看成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，查看帮助信息   expect:查看成功-----'
        self.log.info(text)

        param_list1 = ['h', 'help']
        cmd = f'''dsscmd -i'''
        for param in param_list1:
            execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd1)
            res1 = self.node.sh(execute_cmd1).result()
            self.log.info(res1)
            self.assertIn('Usage: [command] [OPTIONS]', res1, '查看帮助信息失败')

        text = '-----step2:进入交互式模式，查看版本信息   expect:查看成功-----'
        self.log.info(text)

        param_list2 = ['v', 'version']
        for param in param_list2:
            execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd2)
            res2 = self.node.sh(execute_cmd2).result()
            self.log.info(res2)
            self.assertIn('compiled', res2, '查看版本信息失败')

        text = '-----step3:进入交互式模式，查看详细帮助信息   expect:查看成功-----'
        self.log.info(text)

        param_list3 = ['a', 'all']
        for param in param_list3:
            execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                         expect <<EOF
                         set timeout 3
                         spawn {cmd}
                         expect "dsscmd>"
                         send "{param}\\n"
                         expect "dsscmd>"
                         send "exit\\n"
                         expect eof\n''' + '''EOF'''
            self.log.info(execute_cmd3)
            res3 = self.node.sh(execute_cmd3).result()
            self.log.info(res3)
            self.assertIn('Usage: [command] [OPTIONS]', res3, '查看版本信息失败')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
